
package com.insigma.business.qrcode.soldier._bff.impl;

import cn.hutool.core.bean.BeanUtil;
import com.insigma.business.qrcode.common._consumer.CommonAdminAreaConsumer;
import com.insigma.business.qrcode.common._consumer.CommonQaInfoConsumer;
import com.insigma.business.qrcode.common.dto.CommonQaInfoDTO;
import com.insigma.business.qrcode.common.dto.CommonQaInfoInsertRequest;
import com.insigma.business.qrcode.common.dto.CommonQaInfoSearchRequest;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.common.util.BizCheckUtil;

import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.UserTypeEnum;
import com.insigma.business.qrcode.soldier._bff.CommonQaInfoBFFProvider;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.CommonQaInfoUpdateRequest;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Component("commonQaInfoBFFProvider")
public class CommonQaInfoBFFProviderImpl extends BaseProviderImpl implements CommonQaInfoBFFProvider {

    @Resource
    private CommonQaInfoConsumer commonQaInfoConsumer;
    @Resource
    private CommonAdminAreaConsumer commonAdminAreaConsumer;

    @Override
    public BasePagingResponse<List<CommonQaInfoDTO>> searchCommonQaInfoOfSoldier(CommonQaInfoSearchRequest searchRequest) {

        BizCheckUtil.checkNotBlank(searchRequest.getAdminAreaCode(), "地区码不为空！");
        searchRequest.setSortKeyValueJoint("seqNo_asc");
        return commonQaInfoConsumer.searchCommonQaInfo(searchRequest);
    }


    @Override
    public BasePagingResponse<List<CommonQaInfoDTO>> searchCommonQaInfo(final CommonQaInfoSearchRequest searchRequest) {

        final BasePagingResponse<List<CommonQaInfoDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页CommonQaInfo的请求参数不能为空");
            }

            @Override
            public void executeService() {
                searchRequest.setAdminAreaCode(commonAdminAreaConsumer.checkIsChildAdminAreaCode(searchRequest.getAdminAreaCode()));
                searchRequest.setSortKeyValueJoint("seqNo_asc");

                searchRequest.setAdminAreaCodeList(AdminAreaCodeUtils.getAllFatherCodes(searchRequest.getAdminAreaCode()));
                searchRequest.setAdminAreaCode(null);
                BasePagingResponse<List<CommonQaInfoDTO>> listBasePagingResponse = commonQaInfoConsumer.searchCommonQaInfo(searchRequest);
                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
             }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_QA_INFO_SEARCH;
            }
        });

        return basePagingResponse;
    }



    @Override
    public BaseResponse<CommonQaInfoDTO> getCommonQaInfo(final BaseGetRequest baseGetRequest){

        final BaseResponse<CommonQaInfoDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条CommonQaInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<CommonQaInfoDTO> commonQaInfoResponse = commonQaInfoConsumer.getCommonQaInfo(baseGetRequest);
                BeanUtil.copyProperties(commonQaInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_QA_INFO_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertCommonQaInfo(final CommonQaInfoInsertRequest insertRequest){

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增CommonQaInfo的请求参数不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildCreatorOfGov(insertRequest);

                //insertRequest.setAdminAreaCode(commonAdminAreaConsumer.checkIsChildAdminAreaCode(null));
                BaseResponse<String> commonQaInfoResponse = commonQaInfoConsumer.insertCommonQaInfo(insertRequest);
                BeanUtil.copyProperties(commonQaInfoResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_QA_INFO_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> updateCommonQaInfo(final CommonQaInfoUpdateRequest updateRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改CommonQaInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildModifierOfGov(updateRequest);
                BaseResponse<Integer> commonQaInfoResponse = commonQaInfoConsumer.updateCommonQaInfo(updateRequest);
                BeanUtil.copyProperties(commonQaInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_QA_INFO_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteCommonQaInfo(final BaseDeleteRequest deleteRequest){

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除CommonQaInfo的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String)deleteRequest.getId(), "id不能为空");
            }
            @Override
            public void executeService() {
                SessionUtils.buildDeleteOfGov(deleteRequest);
                BaseResponse<Integer> commonQaInfoResponse = commonQaInfoConsumer.deleteCommonQaInfo(deleteRequest);
                BeanUtil.copyProperties(commonQaInfoResponse, baseResponse);
            }
            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.COMMON_QA_INFO_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }
}
